import commands
import codecs
import time
import sys
import re
import utils


def chromeDeobLink(url, outfilename):
	chrome = "/space/webcrawl-malware/chromium/src/out/Release/chrome"
	# chrome = "/home/ruian/chromium/src/out/Release/chrome "
	# para=" --no-sandbox -incognito "
	# --disable-restore-session-state 
	para = " --allow-outdated-plugins "
	outpart = ' > '
	backend = ' &'

	# run chrome and redirect the js from stdout to file
	cmddeob = chrome + para + '\'' + url + '\'' + outpart + outfilename + backend
	# set standard buffer to 0
	cmdstdbuf = 'stdbuf -o0 '
	# sleep for some time
	cmdsleep = 'sleep 15'
	# kill chrome and restart
	cmdkillchrome = 'killall chrome'
	allcmd = cmdstdbuf + cmddeob + '\n' + cmdsleep + '\n' + cmdkillchrome #disabled for benign sites:  + '\n' + cmdkillchrome
	commands.getoutput(allcmd)


def chromeDeobFile(linkfile, outdir):
	linkf = codecs.open(linkfile, encoding = 'utf-8', mode = 'r')
	for link in linkf:
		url = link.split(',')[1][1:-1]
		outfilename = url.split('.')[0] + '.js'
		chromeDeobLink(url, outdir + outfilename)
	linkf.close()
		

def chromeDeobFileHash(linkfile, outdir):
	linkf = codecs.open(linkfile, encoding = 'utf-8', mode = 'r')
	for link in linkf:
		url, outfilename = utils.urlandhash(link)
		chromeDeobLink(url, outdir + outfilename)
	linkf.close()


def deobustop():
	ustopfile = '../../data/TempData/URLs/alexaustop'
	outdir = '../../data/TempData/JSFileWComments/'
	if len(sys.argv) == 2:
		dir=sys.argv[1]
		ustopfile = dir + '/data/TempData/URLs/alexaustop'
		outdir = dir + '/data/TempData/JSFileWComments/'

if __name__=="__main__":
	deobustop()
	
